package rxtxsample;

import java.io.*;

public class Rxtxsample {

	/**
	 * @param args
	 */
	static SerialComm s;
 //       static ReaderThread rdt;

        public static void main(String[] args) {
            
  //          String fileName="../Testprog/einstein.bmp";     
          String fileName="../Testprog/hft.bmp";     
  //          String fileName="../Testprog/karte.bmp";
            
     BufferedReader in = new BufferedReader(
                         new InputStreamReader( System.in ) );
	  int p = -1;
	  s = new SerialComm();
	  s.getPortList();
	  boolean isOpen = false;

	  System.out.println( "Zahl eingeben, mit Eingabetaste abschliessen" );
	  try {
	    String l = in.readLine();
	    p = Integer.parseInt(l);
	    } catch( IOException ex ) { System.out.println( ex.getMessage() ) ;} ;
	  if (p>-1) isOpen = s.openSio(p, 115200);

	  //if (isOpen) rdt = new ReaderThread(s);
      if (!isOpen) { System.out.println("SIO not open"); return;}
    	  
         FileInputStream fis = null;
         byte[] data = new byte[200000];
         try {

            File file = new File(fileName);
            fis = new FileInputStream(file);
            long size = file.length();
            if (size != 1024*1280/8+0x82) {
                System.out.println("File falsche Länge");
                return;
            }
  
    //        int by, i=0;
              fis.read(data, 0x82, 1024*1280/8);
            } // try
        catch(Exception ex) { System.out.println(ex);};
        if(fis!=null)
            try{ fis.close();} 
                catch(Exception ex) {System.out.println(ex);}
        System.out.println("File loaded");
// Bit Flip und XOR

        for(int i=0;i<1024*1280/8+0x82;i++) {
            byte b = 0;
        	for(int q =0;q<8;q++) 
        		if ((data[i] & (1 << q)) == 0)
        			b |= (0x80 >> q);
            data[i]= b;
            }

        
/*        while (true) {
        	String line = s.getLine(2000);
        	if (line != null)
        		System.out.println(line);
        	else
        		System.out.println("Timeout");
        };*/
        
      if (s.waitfor(new String[]{"IDLE"}, 2000) != 0) {
        	 System.out.println("No Idle");
        	 return;
        	}
      
        s.println("\n");
        try {Thread.sleep(10);} catch(Exception e) {};
        
      	s.println("O\n");
      	
        int result = s.waitfor(new String[]{"CONNECT", "TIO"} , 15000);
        System.out.println("result is "+result);
        if (result == -1) {
          	 System.out.println("Timeout Open");
          	 return;
        	}
        if (result == 1) {
         	 System.out.println("TIO code received"); 
          	 return;
        	}
 // OK received
        for(int zeile = 0 ; zeile < 1280; zeile++) {
            try{
        	s.outputStream.write('L');
            s.outputStream.write((byte) 0xff);
            s.outputStream.write(data, zeile*128, 128);
            s.outputStream.write((byte) 0x0d);
            } catch(Exception e) {};
            String res = s.getLine(3000);
            if (res == null) { System.out.println("Timeout"); return; };
            if (!res.equals("OK")) 
            				{ System.out.println("is not OK"); return; };
            
         }
        result = s.waitfor(new String[]{"COMPLETE", "FAILED"} , 2000);
    	System.out.println( "Fertig" + result);
        
 	} // main

        
}

